package com.lap.auth.adapter.driven.messaging;

import com.lap.auth.domain.user.event.UserAssignedEvent;
import com.lap.auth.domain.user.event.UserCreatedEvent;
import com.lap.auth.domain.user.event.UserDisabledEvent;
import com.lap.auth.domain.user.event.UserEnabledEvent;
import com.lap.auth.domain.user.event.UserUpdateEvent;
import com.lap.framework.tools.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Slf4j
@Component
public class UserEventListener {

  // 还可以异步,这里是确保事务提交后消费事件消息
  @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
  public void onUserEnabled(UserEnabledEvent event) {
    log.info("enabled user:{}", JsonUtil.prettyOut(event));
  }

  @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
  public void onUserDisabled(UserDisabledEvent event) {
    log.info("disabled user:{}", JsonUtil.prettyOut(event));
  }

  @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
  public void onUserCreated(UserCreatedEvent event) {
    log.info("created user:{}", JsonUtil.prettyOut(event));
  }

  @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
  public void onUserUpdated(UserUpdateEvent event) {
    log.info("updated user:{}", JsonUtil.prettyOut(event));
  }

  @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
  public void onUserAssigned(UserAssignedEvent event) {
    log.info("assigned user:{}", JsonUtil.prettyOut(event));
  }
}
